package com.tpp.base.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tpp.base.feign.ImageFeign;
import com.tpp.base.mapper.CinemaMapper;
import com.tpp.base.mapper.ItemMapper;
import com.tpp.base.mapper.ItemTypeMapper;
import com.tpp.comm.pojo.TpCinema;
import com.tpp.comm.pojo.TpImage;
import com.tpp.comm.pojo.TpItem;
import com.tpp.comm.pojo.TpItemType;
import com.tpp.comm.util.TpConst;
import com.tpp.base.vo.FloorHashKey;
import com.tpp.base.vo.ItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class IndexFloorsTask {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ItemMapper itemMapper;
    @Resource
    private ItemTypeMapper itemTypeMapper;

    @Resource
    private ImageFeign imageFeign;

    @Resource
    private CinemaMapper cinemaMapper;

    /**
     * 初始化redis楼层数据
     */
    @Scheduled(initialDelay = 3000,fixedDelay = 1000*60*60*60*24)
    public void transferDataToRedis(){
        BoundHashOperations<String, FloorHashKey, List<ItemVo>> hashOps = redisTemplate.boundHashOps(TpConst.Redis.INDEX_FLOOR_KEY);
        LambdaQueryWrapper<TpItemType> queryWrapper = new QueryWrapper<TpItemType>().lambda();
        queryWrapper.isNotNull(TpItemType::getKey);
        queryWrapper.orderByAsc(TpItemType::getKey);
        List<TpItemType> tpItemTypes = itemTypeMapper.selectList(queryWrapper);
        tpItemTypes.stream().forEach(tpItemType -> {
            FloorHashKey floorHashKey = new FloorHashKey();
            floorHashKey.setFloor(Integer.valueOf(tpItemType.getKey()));
            floorHashKey.setItemTypeId(tpItemType.getId());
            floorHashKey.setItemTypeName(tpItemType.getAliasName());
            //
            LambdaQueryWrapper<TpItem> itemWrapper = Wrappers.lambdaQuery();
            itemWrapper.eq(TpItem::getItemType1Id,floorHashKey.getItemTypeId());
            List<TpItem> tpItems = itemMapper.selectList(itemWrapper);
            List<ItemVo> items = tpItems.stream().map(tpItem -> {
                ItemVo itemVo = new ItemVo();
                BeanUtils.copyProperties(tpItem,itemVo);
                TpImage tpImage = imageFeign.findImage(tpItem.getId(), 1, 2);
                itemVo.setImgUrl(tpImage.getImgUrl());
                TpCinema tpCinema = cinemaMapper.selectById(tpItem.getCinemaId());
                itemVo.setLongitude(tpCinema.getLongitude());
                itemVo.setLatitude(tpCinema.getLatitude());
                itemVo.setAreaId(tpCinema.getAreaId());
                itemVo.setAreaName(tpCinema.getAreaName());
                return itemVo;
            }).toList();
            hashOps.put(floorHashKey,items);
        });
    }

}
